package cn.glenn.weibo0521.controller;

import cn.glenn.weibo0521.base.respone.JsonResult;
import cn.glenn.weibo0521.mapper.WeiboMapper;
import cn.glenn.weibo0521.pojo.enity.Weibo;
import cn.glenn.weibo0521.pojo.vo.UserVO;
import cn.glenn.weibo0521.pojo.vo.WeiboDetailVO;
import cn.glenn.weibo0521.pojo.vo.WeiboIndexVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Size;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/v1/weibo/")
@Validated
@Tag(name = "02-微博模块",description = "02-微博模块")
public class WeiboController {
    @Autowired
    private WeiboMapper weiboMapper;

    @PostMapping("insert")
    @Parameter(name = "content",description = "微博正文")
    @Operation(summary = "发布微博")
    @ApiOperationSupport(order = 2)
    public JsonResult postWeibo(@Valid @Size(min = 10,max = 500,message = "正文必须在10-500字") String content, HttpSession session) {
        Weibo weibo = new Weibo();
        UserVO user = (UserVO) session.getAttribute("user");
        weibo.setContent(content);
        weibo.setCreateTime(new Date());
        weibo.setUserId(user.getId());
        int result = weiboMapper.insertWeibo(weibo);
        log.debug("result:{}", result);
        return JsonResult.success();
    }

    @GetMapping("selectIndex")
    @Operation(summary = "查询微博")
    @ApiOperationSupport(order = 3)
    public JsonResult weiboIndex(){
        List<WeiboIndexVO> list = weiboMapper.listWeibo();
        return JsonResult.success(list);
    }


    @GetMapping("selectById")
    @Operation(summary = "微博详情")
    @ApiOperationSupport(order = 4)
    public JsonResult weiboDetail(@Valid @Range(min = 100,max = 500,message = "微博ID必须在100-500之间") Long id){
        WeiboDetailVO weiboDetailVO = weiboMapper.getWeiboDetail(id);
        return JsonResult.success(weiboDetailVO);
    }




}
